import 'package:extended_image/extended_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_application_1/plugins/extended_image.dart';
import 'package:flutter_application_1/utils/utils.dart';

import 'price_widget.dart';

class InfoItemData {
  String cover;
  String? name;
  String? price;

  InfoItemData({
    required this.cover,
    this.name,
    this.price,
  });
}

class InfoItem extends StatefulWidget {
  final InfoItemData data;
  final Color primaryColor;
  final bool isFull;
  final Function? onTap;

  InfoItem({
    required this.data,
    this.primaryColor = Colors.white,
    this.isFull = false,
    this.onTap,
  });

  @override
  _InfoItemState createState() => _InfoItemState();
}

class _InfoItemState extends State<InfoItem> {
  double size = (750 * Utils.rpx - 90) / 2;
  double boderWidth = 5;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        if (widget.onTap != null) {
          widget.onTap!();
        }
      },
      child: Container(
        width: widget.isFull ? double.infinity : size,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // 图片、价格
            ClipRRect(
              borderRadius: BorderRadius.circular(8),
              child: Stack(
                alignment: Alignment.bottomCenter,
                children: [
                  // 图片
                  Container(
                    decoration: BoxDecoration(
                      border: Border.all(
                        color: widget.primaryColor,
                        width: boderWidth,
                        style: BorderStyle.solid,
                      ),
                    ),
                    child: ExtendedImage.network(
                      widget.data.cover,
                      width: widget.isFull
                          ? double.infinity
                          : size - boderWidth * 2,
                      height: size - boderWidth * 2,
                      fit: BoxFit.cover,
                      loadStateChanged: commonLoadStateChanged,
                    ),
                  ),
                  // 价格
                  widget.data.price != null
                      ? Container(
                          height: size / 4,
                          color: Color.fromRGBO(255, 255, 255, 0.6),
                          padding: EdgeInsets.symmetric(
                            vertical: 4,
                            horizontal: boderWidth * 2,
                          ),
                          child: Row(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              PriceWidget(
                                price: widget.data.price!,
                              ),
                            ],
                          ),
                        )
                      : SizedBox(),
                ],
              ),
            ),
            // 名称
            widget.data.name != null
                ? Container(
                    padding: EdgeInsets.all(3),
                    child: Text(
                      widget.data.name!,
                      maxLines: 2,
                      overflow: TextOverflow.ellipsis,
                      style: TextStyle(
                        fontSize: 15,
                      ),
                    ),
                  )
                : SizedBox(),
          ],
        ),
      ),
    );
  }
}
